package com.atguigu.exam.controller;

import com.atguigu.exam.common.Result;
import com.atguigu.exam.entity.Banner;
import com.atguigu.exam.service.BannerService;
import com.atguigu.exam.service.FileUploadService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;
//解决跨域问题注解
@CrossOrigin
@Tag(name = "轮播图管理")
@RestController
@RequestMapping("/api/banners")
public class BannerController {
    @Autowired
    private BannerService bannerService;
    @Autowired
    private FileUploadService fileUploadService;

    @Operation(summary = " 查询激活状态轮播图接口")
    @GetMapping("/active")
    public Result getActive(){
        //创建LambdaQueryWrapper对象
        LambdaQueryWrapper<Banner> LambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper.eq(Banner::getIsActive,1);
        LambdaQueryWrapper.orderByAsc(Banner::getSortOrder);
        List<Banner> list = bannerService.list(LambdaQueryWrapper);
        return Result.success(list);
    }
    @Operation(summary = "获取轮播图详情接口")
    @GetMapping("/{id}")
    public Result getById(@PathVariable Long id){
        Banner byId = bannerService.getById(id);
        return Result.success(byId);
    }
    @Operation(summary = "查询所有轮播图接口")
    @GetMapping("/list")
    public Result list(){
        //创建LambdaQueryWrapper对象,是MyBatis-Plus 提供的查询条件构造器
        LambdaQueryWrapper<Banner> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByDesc(Banner::getSortOrder);
        List<Banner> list = bannerService.list(lambdaQueryWrapper);
        return Result.success(list);
    }
    @Operation(summary = "添加轮播图")
    @PutMapping("/add")
    public Result add(@RequestBody Banner banner){
        boolean save = bannerService.save(banner);
        if(save){
            return Result.success("添加成功");
        }else{
            return Result.error("添加失败");
        }
    }
    @Operation(summary = "更新轮播图")
    @PostMapping("/update")
    public Result update(@RequestBody Banner banner){
        boolean b = bannerService.updateById(banner);
        if (b){
            return Result.success("更新成功");
        }else{
            return Result.error("更新失败");
        }
    }
    @Operation(summary = "删除轮播图")
    @DeleteMapping("/delete/{id}")
    public Result delete(@PathVariable Long id){
        boolean b = bannerService.removeById(id);
        if (b){
            return Result.success("删除成功");
        }else{
            return Result.error("删除失败");
        }
    }
    @Operation(summary = "添加或更新轮播图")
    @RequestMapping(value = {"/add","/update"},method = {RequestMethod.POST,RequestMethod.PUT})
    public Result saveOrUpdate(@RequestBody Banner banner){
        boolean b = bannerService.saveOrUpdate(banner);
        if (b){
            return Result.success("添加或更新成功");
        }else{
            return Result.error("添加或更新失败");
        }
    }
    @Operation(summary = "上传轮播图")
    @PostMapping("/upload-image")
    public Result upload(MultipartFile file){
        String s = fileUploadService.uploadFile(file);
        return Result.success(s,"上传成功"  );
    }
    @Operation(summary = "切换轮播图状态")
    @PutMapping("toggle/{id}")
    public Result toggle(@PathVariable Long id , @RequestParam Boolean isActive){
        LambdaUpdateWrapper<Banner> lambdaUpdateWrapper = new LambdaUpdateWrapper<>();
        lambdaUpdateWrapper.eq(Banner::getId,id)
                .set(Banner::getIsActive, isActive);
        boolean update = bannerService.update(lambdaUpdateWrapper);
        if (update){
            return Result.success("切换成功");
        }else{
            return Result.error("切换失败");
        }
    }
}
